﻿/*Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. 
4  See LICENSE in the project root for license information */

var dialog;
var dialogW = 40;
var dialogH = 68;
function eventHandler(arg) {
    //Required, call event.completed to let the platform know you are done processing.
    clickEvent.completed();
    // In addition to general system errors, there are 2 specific errors 
    // and one event that you can handle individually.
    switch (arg.error) {
        case 12002:
            console.log("Cannot load URL, no such page or bad URL syntax.");
            break;
        case 12003:
            console.log("HTTPS is required.");
            break;
        case 12006:
            // The dialog was closed, typically because the user the pressed X button.
            console.log("Dialog closed by user");
            break;
        default:
            console.log("Undefined error in dialog window");
            break;
    }
}

/**
 * 提取数据
 * @param {any} event
 */
function getData(event) {
    clickEvent = event;
    localStorage["isNewNotUpdateAll"] = "New";

    showFillDialog();
}

/**
 * 提取数据向导页面
 */
function showFillDialog() {
    ////IMPORTANT: IFrame mode only works in Online (Web) clients. Desktop clients (Windows, IOS, Mac) always display as a pop-up inside of Office apps. 
    //Office.context.ui.displayDialogAsync(url,
    //    { height: dialogH, width: dialogW, displayInIframe: true }, getDataBack);

    request("600600", 3, 20, 5, 10000,1,1);
}

/**
 * 提取数据回调处理
 */
function getDataBack(result) {
    if (result.status == Office.AsyncResultStatus.Failed) {
        return;
    }

    dialog = result.value;

    /*Messages are sent by developers programatically from the dialog using office.context.ui.messageParent(...)*/
    dialog.addEventHandler(Office.EventType.DialogMessageReceived, getDataHandler);

    /*Events are sent by the platform in response to user actions or errors. For example, the dialog is closed via the 'x' button*/
    dialog.addEventHandler(Office.EventType.DialogEventReceived, eventHandler);
}

/**
 * 提取数据填充处理
 */
function getDataHandler(input,result) {
    //dialog.close();
    //clickEvent.completed();
 
    Excel.run(function (context) {
        return context.sync()
            .then(function () {
                fillSheetCatelog(context, input, result.stockInfo);
            })
            .then(context.sync)
            .then(function () {
                fillSheetCoreAssumption(context, input, result);
            })
            .then(context.sync)
            .then(function () {
                
            })
            .then(context.sync)
            .then(function () {

            })
            .then(context.sync)
            .then(function () {

            })
            .then(context.sync)
            .then(function () {

            })
            .then(context.sync)
            .then(function () {

            })
            .then(context.sync)
            .then(function () {

            })
            .then(context.sync)
            .then(function () {

            }).then(context.sync)
            .then(function () {

            }).then(context.sync)
            .then(function () {

            })
            .then(context.sync)
            .then(function () {

            })
            .then(context.sync)
            .then(function () {

            })
            .then(context.sync)
            .then(function () {
                clickEvent.completed();
            }).catch(function (e) {
                clickEvent.completed();
                console.log(e);
            });
    }).catch(function (e) {
        clickEvent.completed();
        console.log(e);
    });
}
 
function request(code, year, quarter, detailYear, unit, reportCate, reportType) {
     
    var param = JSON.parse("{}");
    param.year = year;
    param.quarter = quarter;
    param.detailYear = detailYear;
    param.unit = unit;
    param.reportCate = reportCate;
    param.reportType = reportType;

    //1.股票信息
    var stocinfokUrl = kURL_StockInfo + "?code=" + code;
    //2.利润表
    var incomestmtUrl = kURL_Incomestmt + "?code=" + code + "&type=annual&nums=10";
    //3.主营业务构成
    var mainrevenuedetailUrl = kURL_Mainrevenuedetail + "?code=" + code + "&nums=10";
    //4.资产减值准备期末值
    var provisiondiminutionUrl = kURL_Provisiondiminution + "?code=" + code + "&type=annual&nums=10";
    //5.分红送股
    var stkexUrl = kURL_Stkex + "?code=" + code + "&type=annual&nums=10";
    //6.现金流量表
    var cashflowstmtUrl = kURL_Cashflowstmt + "?code=" + code + "&type=annual&nums=10";
    //7.一致预期数据
    var estimatevalueUrl = kURL_Estimatevalue + "?code=" + code;
    //8.资产负债表
    var balancesheetUrl = kURL_Balancesheet + "?code=" + code + "&type=annual&nums=10";
    //9.财务费用
    var financefeeUrl = kURL_Financefee + "?code=" + code + "&type=annual&nums=10";
    //10.固定资产详细
    var assetsdetailUrl = kURL_Assetsdetail + "?code=" + code + "&type=annual&nums=10";

    var stockInfo;
    var incomestmt;
    var mainrevenuedetail;
    var provisiondiminution;
    var stkex;
    var cashflowstmt;
    var estimatevalue;
    var balancesheet;
    var financefee;
    var assetsdetail;

    $.getJSON(stocinfokUrl).then(function (json) {
        if (json.code == 200) {
            stockInfo = json.data;
            return $.getJSON(incomestmtUrl);
        } else {
            throw json.msg;
        }
    }).then(function (json) {
        if (json.code == 200) {
            incomestmt = json.data;
            return $.getJSON(mainrevenuedetailUrl);
        } else {
            throw json.msg;
        }
    }).then(function (json) {
        if (json.code == 200) {
            mainrevenuedetail = json.data;
            return $.getJSON(provisiondiminutionUrl);
        } else {
            throw json.msg;
        }
    }).then(function (json) {
        if (json.code == 200) {
            provisiondiminution = json.data;
            return $.getJSON(stkexUrl);
        } else {
            throw json.msg;
        }
    }).then(function (json) {
        if (json.code == 200) {
            stkex = json.data;
            return $.getJSON(cashflowstmtUrl);
        } else {
            throw json.msg;
        }
    }).then(function (json) {
        if (json.code == 200) {
            cashflowstmt = json.data;
            return $.getJSON(estimatevalueUrl);
        } else {
            throw json.msg;
        }
    }).then(function (json) {
        if (json.code == 200) {
            estimatevalue = json.data;
            return $.getJSON(balancesheetUrl);
        } else {
            throw json.msg;
        }
    }).then(function (json) {
        if (json.code == 200) {
            balancesheet = json.data;
            return $.getJSON(financefeeUrl);
        } else {
            throw json.msg;
        }
    }).then(function (json) {
        if (json.code == 200) {
            financefee = json.data;
            return $.getJSON(assetsdetailUrl);
        } else {
            throw json.msg;
        }
    }).then(function (json) {
        if (json.code == 200) {
            assetsdetail = json.data;
            return null;
        } else {
            throw json.msg;
        }
    }).then(function (j) {

        var result = JSON.parse("{}");

        result.stockInfo = stockInfo;
        result.incomestmt = incomestmt;
        result.mainrevenuedetail = mainrevenuedetail;
        result.provisiondiminution = provisiondiminution;
        result.stkex = stkex;
        result.cashflowstmt = cashflowstmt;
        result.estimatevalue = estimatevalue;
        result.balancesheet = balancesheet;
        result.financefee = financefee;
        result.assetsdetail = assetsdetail;

        getDataHandler(param, result);

        clickEvent.completed();
        }).catch(function (jqXHR, textStatus, errorThrown) {
            console.log(jqXHR)

            clickEvent.completed();
    });

}
